package com.softpo.messagedigest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity {

    private String pw = "softpo123";
    //浏览器运算结果：	6a2ac3dd2d6b159ab9055c5c75e16089

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //button点击事件，进行运算
    public void messageDigest(View view) {
        switch (view.getId()) {
            case R.id.md5://32位
                String md5 = messageDigest("MD5", pw);

                Log.d("flag", "----------------->messageDigest: 经过MD5运算后结果： "
                        +md5);
                break;
            case R.id.sha1://40位
                String sha1 = messageDigest("SHA-1", pw);
                Log.d("flag", "----------------->messageDigest: 经过SHA-1运算结果： "
                        +sha1);
                break;
        }
    }

    //运算方法，就创建了
    private String messageDigest(String type, String data) {

        try {
            //1、创建消息摘要的对象
            MessageDigest messageDigest = MessageDigest.getInstance(type);

            //2、调用对象的方法，进行运算

            //将要运算的数据，加载给MessageDigest
            messageDigest.update(data.getBytes());

            //hash值
            //the array of bytes for the resulting hash value.
            byte[] digest = messageDigest.digest();

            //3、将字节数组转换成该样式：ff5acd05af9a5d200ed9b742cc60a1df
            //	7eca689f0d3389d9dea66ae112e5cfd7
            //	5d41402abc4b2a76b9719d911017c592
            //  fd 25 2e 58 34 fd 19 03 9b 38 84 e1 3a f0 5a 81
            //规律：0到9，a到f：十六进制表示数据
            //3.1 打印运算后的数据
//            Log.d("flag", "----------------->messageDigest: hash值的字节数组长度： "
//                    + digest.length);

//            for (int i = 0; i < digest.length; i++) {
////                Log.d("flag", "----------------->messageDigest: " +digest[i]);
//                Log.d("flag", "----------------->messageDigest：二进制： " + Integer.toBinaryString(digest[i]));
//            }

            //需要将字节数组，转换成16进制表示

            /**
             * 11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: hash值的字节数组长度： 16
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 106
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 42
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: -61
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: -35
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 45
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 107
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 21
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: -102
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: -71
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 5
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 92
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 92
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 117
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: -31
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: 96
             11-25 11:26:25.895 8659-8659/? D/flag: ----------------->messageDigest: -119
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest: hash值的字节数组长度： 16
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 1101010 &11111111
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 00101010
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 11111111111111111111111111000011 &11111111 --＞11000011
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 11111111111111111111111111011101
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 00101101
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 1101011
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 00010101
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 11111111111111111111111110011010
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 11111111111111111111111110111001
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 00000101
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 1011100
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 1011100
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 1110101
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 11111111111111111111111111100001
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 1100000
             11-25 11:31:40.625 8877-8877/? D/flag: ----------------->messageDigest：二进制： 11111111111111111111111110001001
             */

            //二进制 00000000000000000000000011111111 与运算

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < digest.length; i++) {
                //0xFF = 二进制：1111  1111 == 0xFF
                 //F == 15 二进制 1111 = 15
                int num = digest[i] & 0xFF;

                if(num<=0xF){
                    builder.append(0);
                }
                String result = Integer.toHexString(num);

                builder.append(result);
            }

            //运算后的数据，返回
            return builder.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
